package com.hontech.bvapp2.utils
import android.app.Activity
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.pm.PackageManager
import android.os.SystemClock
import android.view.inputmethod.InputMethodManager
import com.hontech.bvapp2.app.AppContext
import java.io.PrintWriter
import java.net.NetworkInterface

private const val MAC_IF_NAME = "eth0"

private fun getMac(): String {

    try {
        val ns = NetworkInterface.getNetworkInterfaces()
        for (n in ns) {
            val hard = n.hardwareAddress ?: continue
            if (n.name == MAC_IF_NAME) {
                return hard.formatMac()
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return "无法获取MAC地址"
}

val macAddr = getMac()

const val APP_ID = "com.hontech.bvapp2"

fun hideSoftKey(activity: Activity) {
    val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    if (!imm.isActive) {
        return
    }
    val view = activity.currentFocus ?: return
    imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}

val PackageName = AppContext.packageName
val Version = AppContext.packageManager.getPackageInfo(PackageName, 0).versionName

fun installApk2(path: String) {
    val rt = Runtime.getRuntime().exec("su")

    val pw = PrintWriter(rt.outputStream)
    pw.println("pm install -r -t $path")
    pw.flush()
    pw.close()
    val v = rt.waitFor()

    if (v != 0) {
        throw IllegalStateException("安装失败")
    }
}


private fun rootGrantPermission(name: String) {
    val runtime = Runtime.getRuntime()
        .exec("su")

    val pw = PrintWriter(runtime.outputStream)
    pw.write("pm grant $APP_ID $name")
    pw.flush()
    pw.close()
    val v = runtime.waitFor()
    if (v != 0) {
        throw IllegalStateException("无法获取权限:$name")
    }
}

private fun checkPermission(name: String): Boolean {
    val ret = AppContext.checkSelfPermission(name)
    return ret == PackageManager.PERMISSION_GRANTED
}

suspend fun grantPermission(name: String) {
    if (checkPermission(name)) {
        return
    }
    runIO { rootGrantPermission(name) }
}

fun stringById(id: Int) = AppContext.resources.getString(id)
fun stringsById(id: Int) = AppContext.resources.getStringArray(id)
fun intsById(id: Int) = AppContext.resources.getIntArray(id)
fun dimenById(id: Int) = AppContext.resources.getDimension(id).toInt()

fun restartAppWithDelay() {
    val intent = AppContext.packageManager.getLaunchIntentForPackage(AppContext.packageName)
    val restartIntent = PendingIntent.getActivity(
        AppContext, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
    )
    val alarmManager = AppContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
    alarmManager.set(
        AlarmManager.ELAPSED_REALTIME,
        SystemClock.elapsedRealtime() + 100,
        restartIntent
    )
    Runtime.getRuntime().exit(0) // ${stringById(R.string.t1733460848_481)}
}






























